home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / nfsmount / mount.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-10-10  |  6.5 KB  |  285 lines

  1. /*
  2.  * mount.c --
  3.  * 
  4.  *    Utility procedures for using the Sun Mount protocol.
  5.  *
  6.  * Copyright 1988 Regents of the University of California
  7.  * Permission to use, copy, modify, and distribute this
  8.  * software and its documentation for any purpose and without
  9.  * fee is hereby granted, provided that the above copyright
  10.  * notice appear in all copies.  The University of California
  11.  * makes no representations about the suitability of this
  12.  * software for any purpose.  It is provided "as is" without
  13.  * express or implied warranty.
  14.  */
  15. #ifndef lint
  16. static char rcsid[] = "$Header: /a/newcmds/nfsmount/RCS/mount.c,v 1.5 89/10/10 13:17:09 douglis Exp $ SPRITE (Berkeley)";
  17. #endif not lint
  18.  
  19. #include "stdio.h"
  20.  
  21. #include "nfs.h"
  22.  
  23.  
  24. /*
  25.  *----------------------------------------------------------------------
  26.  *
  27.  * Nfs_MountInitClient --
  28.  *
  29.  *    Set up the CLIENT data structure needed to do SUN RPC to the
  30.  *    mount program running on a particular host.
  31.  * 
  32.  * Results:
  33.  *    None.
  34.  *
  35.  * Side effects:
  36.  *    Calls clnt_create which sets up sockets and other related state.
  37.  *
  38.  *----------------------------------------------------------------------
  39.  */
  40. CLIENT *
  41. Nfs_MountInitClient(host)
  42.     char *host;
  43. {
  44.     register CLIENT *clnt;
  45.     int voidArg;
  46.     VoidPtr voidRes;
  47.  
  48.     clnt = clnt_create(host, MOUNTPROG, MOUNTVERS, "udp");
  49.     if (clnt == (CLIENT *)NULL) {
  50.     clnt_pcreateerror(host);
  51.     } else {
  52.     clnt->cl_auth = authunix_create_default();
  53.     voidRes = mountproc_null_1(&voidArg, clnt);
  54.     if (voidRes == (VoidPtr)NULL) {
  55.         clnt_perror(clnt, "mountproc_null_1");
  56.     } else if (pdev_Trace) {
  57.         printf("Null RPC to Mount service at %s succeeded\n", host);
  58.     }
  59.     }
  60.     return(clnt);
  61. }
  62.  
  63. /*
  64.  *----------------------------------------------------------------------
  65.  *
  66.  * Nfs_MountTest --
  67.  *
  68.  *    Test the mount protocol by making a null rpc to the mount server.
  69.  * 
  70.  * Results:
  71.  *    None.
  72.  *
  73.  * Side effects:
  74.  *    Prints an error and exits the process if the RPC fails.
  75.  *
  76.  *----------------------------------------------------------------------
  77.  */
  78. void
  79. Nfs_MountTest(clnt, host)
  80.     CLIENT *clnt;
  81.     char *host;
  82. {
  83.     char voidArg;
  84.     VoidPtr voidRes;
  85.  
  86.     voidRes = mountproc_null_1(&voidArg, clnt);
  87.     if (voidRes == (VoidPtr)NULL) {
  88.     clnt_perror(clnt, "mountproc_null_1");
  89.     exit(1);
  90.     } else {
  91.     extern int pdev_Trace;
  92.     if (pdev_Trace) {
  93.         printf("Null RPC to MOUNTPROG at %s succeeded\n", host);
  94.     }
  95.     }
  96. }
  97.  
  98. /*
  99.  *----------------------------------------------------------------------
  100.  *
  101.  * Nfs_Mount --
  102.  *
  103.  *    Called to mount a NFS filesystem.  This does a mount RPC and returns
  104.  *    the nfs_fh that is returned from the NFS server.
  105.  * 
  106.  * Results:
  107.  *    None.
  108.  *
  109.  * Side effects:
  110.  *    None here, but the server does remember that we've mounted from it.
  111.  *
  112.  *----------------------------------------------------------------------
  113.  */
  114. nfs_fh *
  115. Nfs_Mount(clnt, rfs)
  116.     CLIENT *clnt;
  117.     dirpath rfs;
  118. {
  119.     register fhstatus *statusPtr;
  120.     register nfs_fh *handlePtr;
  121.  
  122.     statusPtr = mountproc_mnt_1(&rfs, clnt);
  123.     if (statusPtr == (fhstatus *)NULL) {
  124.     clnt_perror(clnt, "mountproc_mnt_1");
  125.     handlePtr = (nfs_fh *)NULL;
  126.     } else if (statusPtr->fhs_status != 0) {
  127.     errno = statusPtr->fhs_status;
  128.     perror(rfs);
  129.     handlePtr = (nfs_fh *)NULL;
  130.     } else {
  131.     /*
  132.      * The mount.x protocol definition and the nfs_prot.x protocol def
  133.      * have slightly different ways of defining a "file handle".  Both
  134.      * are FHSIZE bytes of opaque data, however, so we can safely copy
  135.      * the "fhandle" returned by the mount in to a "nfs_fh" used
  136.      * by the nfs routines.
  137.      */
  138.     handlePtr = (nfs_fh *)malloc(sizeof(nfs_fh));
  139.     bcopy((char *)statusPtr->fhstatus_u.fhs_fhandle, (char *)handlePtr,
  140.         FHSIZE);
  141.     }
  142.  
  143.     return(handlePtr);
  144. }
  145.  
  146. /*
  147.  *----------------------------------------------------------------------
  148.  *
  149.  * Nfs_MountDump --
  150.  *
  151.  *    This dumps out what filesystems have been mounted by the client.
  152.  *    To find out, this does an RPC to the server to which we are bound.
  153.  * 
  154.  * Results:
  155.  *    None.
  156.  *
  157.  * Side effects:
  158.  *    Print statements.
  159.  *
  160.  *----------------------------------------------------------------------
  161.  */
  162. void
  163. Nfs_MountDump(clnt)
  164.     CLIENT *clnt;
  165. {
  166.     register mountlist *listPtr;
  167.     VoidPtr voidArg;
  168.  
  169.     listPtr = mountproc_dump_1(&voidArg, clnt);
  170.     if (listPtr == (mountlist *)NULL) {
  171.     clnt_perror(clnt, "mountproc_dump_1");
  172.     return;
  173.     }
  174.     printf("Mount List\n");
  175.     do {
  176.     printf("%s:%s\n", listPtr->ml_hostname, listPtr->ml_directory);
  177.     listPtr = listPtr->ml_next;
  178.     } while (listPtr);
  179.  
  180.     return;
  181. }
  182.  
  183. /*
  184.  *----------------------------------------------------------------------
  185.  *
  186.  * Nfs_Exports --
  187.  *
  188.  *    This dumps out what filesystems are exported by the server
  189.  *    to which we are bound.
  190.  * 
  191.  * Results:
  192.  *    None.
  193.  *
  194.  * Side effects:
  195.  *    Print statements.
  196.  *
  197.  *----------------------------------------------------------------------
  198.  */
  199. void
  200. Nfs_Exports(clnt, host)
  201.     CLIENT *clnt;
  202.     char *host;
  203. {
  204.     register exports listPtr, *listPtrPtr;
  205.     register groups groupPtr;
  206.     VoidPtr voidArg;
  207.  
  208.     listPtrPtr = mountproc_export_1(&voidArg, clnt);
  209.     if (listPtrPtr == (exports *)NULL) {
  210.     clnt_perror(clnt, "mountproc_export_1");
  211.     return;
  212.     }
  213.     listPtr = *listPtrPtr;
  214.     printf("Export List of %s\n", host);
  215.     do {
  216.     printf("%s exported to: ", listPtr->ex_dir);
  217.     groupPtr = listPtr->ex_groups;
  218.     while(groupPtr) {
  219.         printf("%s ", groupPtr->gr_name);
  220.         groupPtr = groupPtr->gr_next;
  221.     }
  222.     printf("\n");
  223.     listPtr = listPtr->ex_next;
  224.     } while (listPtr);
  225.  
  226.     return;
  227. }
  228.  
  229. /*
  230.  *----------------------------------------------------------------------
  231.  *
  232.  * Nfs_UnmountAll --
  233.  *
  234.  *    Called to unmount all our NFS filesystems..
  235.  * 
  236.  * Results:
  237.  *    None.
  238.  *
  239.  * Side effects:
  240.  *    Ideally invalidates our fhandles for mounted filesystems,
  241.  *    but probably just erases our name from a list on the server.
  242.  *
  243.  *----------------------------------------------------------------------
  244.  */
  245. void
  246. Nfs_UnmountAll(clnt)
  247.     CLIENT *clnt;
  248. {
  249.     char voidArg;
  250.     VoidPtr voidRes;
  251.  
  252.     voidRes = mountproc_null_1(&voidArg, clnt);
  253.     if (voidRes == (VoidPtr)NULL) {
  254.     clnt_perror(clnt, "mountproc_umntall_1");
  255.     }
  256. }
  257.  
  258. /*
  259.  *----------------------------------------------------------------------
  260.  *
  261.  * Nfs_Unmount --
  262.  *
  263.  *    Called to unmount a NFS filesystem.
  264.  * 
  265.  * Results:
  266.  *    None.
  267.  *
  268.  * Side effects:
  269.  *    Nuke ourselves from the server's mount list.
  270.  *
  271.  *----------------------------------------------------------------------
  272.  */
  273. void
  274. Nfs_Unmount(clnt, rfs)
  275.     CLIENT *clnt;
  276.     dirpath rfs;
  277. {
  278.     VoidPtr voidRes;
  279.  
  280.     voidRes = mountproc_umnt_1(&rfs, clnt);
  281.     if (voidRes == (VoidPtr)NULL) {
  282.     clnt_perror(clnt, "mountproc_umnt_1");
  283.     }
  284. }
  285.